package com.csh.szhm.fragment;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.csh.szhm.R;
import com.csh.szhm.activity.SzhmSortThreeMainActiviy;
import com.csh.szhm.custorm.FlowLayout;
import com.csh.szhm.utils.AppConfig;
import com.csh.szhm.utils.DebugUntil;
import com.csh.szhm.utils.StringUtil;

public class MainFragmentTwo extends MyBaseFragment implements OnClickListener {

	private String[] tempStr1 = new String[] { "餐饮美食", "宾馆酒店", "商品服务", "时尚购物",
			"休闲娱乐" };
	private String[] tempStr2 = new String[] { "芙蓉区", "天心区", "高新区" };

	private LinearLayout mFlowLayoutItemOne;
	private LinearLayout mFlowLayoutItemTwo;
	private EditText mEditText;

	public MainFragmentTwo(int view) {
		super(view);
	}

	@Override
	protected void init() {
		mFlowLayoutItemOne = (LinearLayout) mView
				.findViewById(R.id.flowlayout_item_1);
		mFlowLayoutItemTwo = (LinearLayout) mView
				.findViewById(R.id.flowlayout_item_2);
		mEditText = (EditText) mView.findViewById(R.id.input_search_query);
		mEditText.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
		initSearch();
		setSearch();
	}

	private void setSearch() {
		mEditText.setOnEditorActionListener(new OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_SEARCH
						|| (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
					// do something;
					String temp = mEditText.getText().toString();
					if (!StringUtil.isEmptyString(temp)) {
						startSearchPage();
					} else {
						DebugUntil.createInstance().toastStr("搜索条件不能为空！");
					}

					return true;
				}
				return false;
			}

		});
	}

	private void startSearchPage() {
		String temp = mEditText.getText().toString();
		Intent in = new Intent();
		in.putExtra("search", temp);
		in.putExtra("type_s", -1);
		startMyActivity(in, SzhmSortThreeMainActiviy.class);
	}

	private void initSearch() {
		LayoutInflater inflater = LayoutInflater.from(getActivity());
		FlowLayout layout1 = new FlowLayout(getActivity());
		FlowLayout layout2 = new FlowLayout(getActivity());
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		int jx = (int) (8 * AppConfig.getScreenDensityDpi());
		lp.setMargins(jx, jx, jx, jx);
		for (int i = 0; i < tempStr1.length; i++) {
			TextView txt = (TextView) inflater.inflate(
					R.layout.szhm_search_item_include_layout, null);
			txt.setTag("1-" + i);
			txt.setOnClickListener(this);
			txt.setLayoutParams(lp);
			txt.setText(tempStr1[i]);
			layout1.addView(txt);
		}
		mFlowLayoutItemOne.addView(layout1);
		for (int i = 0; i < tempStr2.length; i++) {
			TextView txt = (TextView) inflater.inflate(
					R.layout.szhm_search_item_include_layout, null);
			txt.setTag("2-" + i);
			txt.setOnClickListener(this);
			txt.setLayoutParams(lp);
			txt.setText(tempStr2[i]);
			layout2.addView(txt);
		}
		mFlowLayoutItemTwo.addView(layout2);
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.search_item_txt_ic) {
			String tag = (String) v.getTag();
			String[] tagArr = tag.split("-");
			int tagItem = Integer.valueOf(tagArr[1]);
			if (tagArr[0].equals("1")) {
				// 上面的搜索
				// DebugUntil.createInstance().toastStr("1->>" + tag);
				click(Integer.valueOf(tagItem));
			} else {
				// DebugUntil.createInstance().toastStr("2->>" + tag);
			}
			// Intent in = new Intent();
			// in.putExtra("type", -1);
			// startMyActivity(in, SzhmSortThreeMainActiviy.class);
		}

	}

	private void click(int i) {
		Intent in = new Intent();
		in.putExtra("type_s", -1);
		switch (i) {
		case 0:
			in.putExtra("type", 1);
			startMyActivity(in, SzhmSortThreeMainActiviy.class);
			break;
		case 1:
			in.putExtra("type", 2);
			startMyActivity(in, SzhmSortThreeMainActiviy.class);
			break;
		case 2:
			in.putExtra("type", 3);
			startMyActivity(in, SzhmSortThreeMainActiviy.class);
			break;
		case 3:
			in.putExtra("type", 5);
			startMyActivity(in, SzhmSortThreeMainActiviy.class);
			break;
		case 4:
			in.putExtra("type", 4);
			startMyActivity(in, SzhmSortThreeMainActiviy.class);
			break;
		}
	}
}
